namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using System;

    public class Category
    {
        private int _Id;
        private string _Name;
        private int _ParentCategoryId;
        private int _ProjectId;

        public Category(int projectId, string name) : this(DefaultValues.GetCategoryIdMinValue(), projectId, DefaultValues.GetCategoryIdMinValue(), name)
        {
        }

        public Category(string name, int categoryId) : this(categoryId, DefaultValues.GetProjectIdMinValue(), DefaultValues.GetCategoryIdMinValue(), name)
        {
        }

        public Category(int projectId, int parentCategoryId, string name) : this(DefaultValues.GetCategoryIdMinValue(), projectId, parentCategoryId, name)
        {
        }

        public Category(int categoryId, int projectId, int parentCategoryId, string name)
        {
            if (parentCategoryId < 0)
            {
                throw new ArgumentOutOfRangeException("parentCategoryId");
            }
            if ((name == null) || (name.Length == 0))
            {
                throw new ArgumentException("name");
            }
            this._Id = categoryId;
            this._ProjectId = projectId;
            this._Name = name;
            this._ParentCategoryId = parentCategoryId;
        }

        public static bool DeleteCategory(int categoryId)
        {
            if (categoryId <= DefaultValues.GetCategoryIdMinValue())
            {
                throw new ArgumentOutOfRangeException("categoryId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteCategory(categoryId);
        }

        public static CategoryCollection GetCategoryByProjectId(int projectId)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetCategoriesByProjectId(projectId);
        }

        public bool Save()
        {
            if (this.Id > DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("categoryId");
            }
            int TempId = DataAccessLayerBaseClassHelper.GetDataAccessLayer().CreateNewCategory(this);
            if (TempId > 0)
            {
                this._Id = TempId;
                return true;
            }
            return false;
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public string Name
        {
            get
            {
                if (this._Name == null)
                {
                    return string.Empty;
                }
                return this._Name;
            }
        }

        public int ParentCategoryId
        {
            get
            {
                return this._ParentCategoryId;
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
        }
    }
}

